package com.itheima.web;

import com.itheima.pojo.User;
import com.itheima.service.Service;
import com.itheima.service.ServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        //解决post请求乱码问题
        request.setCharacterEncoding("UTF-8");


        String verifycode = request.getParameter("verifycode");

//        String checkcode = (String)request.getAttribute("checkcode");
        String checkcode = (String)request.getSession().getAttribute("checkcode");

        if (checkcode.equalsIgnoreCase(verifycode)) {

            //获取数据
            String username = request.getParameter("username");
            String password = request.getParameter("password");

            //创建对象并将得到的数据封装
            User user = new User();
            user.setUsername(username);
            user.setPassword(password);
//调用Service对象进行操作
            Service service = new ServiceImpl();

            User u = service.loginUSer(user);

            if (u == null) {
                response.getWriter().print(false);


            } else {
                response.getWriter().print(true);

            }


        } else {
            response.getWriter().print(0);
        }




    }
}